package co.kr.bymin76.common;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import co.kr.bymin76.dipcalculator.R;
import co.kr.bymin76.model.ModelTitleValue;

public class PopupSpinner extends Activity {

	private ArrayList<ModelTitleValue> mTitleValues = new ArrayList<ModelTitleValue>();
	private ArrayAdapterOption mAdapter;
	private ModelTitleValue mCurrentTitleValue = new ModelTitleValue();
	private String deviceDip;
	
	/** Called when the activity is first created. */
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.popup_spinner);
        
        //
        deviceDip = getDeviceDip();
        
        Intent intent = getIntent();
        String title = intent.getStringExtra("title");
        String confirm = intent.getStringExtra("confirm");
        String currentTitle = intent.getStringExtra("current_title");
        String currentValue = intent.getStringExtra("current_value");
        String titles[] = intent.getStringArrayExtra("titles");
        String values[] = intent.getStringArrayExtra("values");
        
        ModelTitleValue titleValue;
        
		for (int i = 0; i < titles.length; i++) {
			titleValue = new ModelTitleValue();
			titleValue.title = titles[i];
			titleValue.value = values[i];
			
			mTitleValues.add(titleValue);
		}
		
		mCurrentTitleValue.title = currentTitle;
		mCurrentTitleValue.value = currentValue;
		
        TextView textTitle = (TextView) findViewById(R.id.text_title);
        textTitle.setText(title);
        
        Button textOK = (Button) findViewById(R.id.button_ok);
        textOK.setText(confirm);
        textOK.setOnClickListener(ClickListener);
        
        mAdapter = new ArrayAdapterOption(PopupSpinner.this, R.layout.popup_spinner_row, mTitleValues);
        ListView list = (ListView) findViewById(R.id.list_dialog);
        list.setAdapter(mAdapter);
        list.setOnItemClickListener(ItemClickListener);
        

    }

	private OnItemClickListener ItemClickListener = new OnItemClickListener() {

		public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
			mCurrentTitleValue = mTitleValues.get(position);
			mAdapter.notifyDataSetChanged();
		}
		
	};
	
	private OnClickListener ClickListener = new OnClickListener() {

		public void onClick(View v) {
			Intent data = new Intent();
			data.putExtra("title_value", mCurrentTitleValue);
			setResult(RESULT_OK, data);
			finish();
		}
	};

	private class ArrayAdapterOption extends ArrayAdapter<ModelTitleValue> {

		private ArrayList<ModelTitleValue> titleValues;
		
		public ArrayAdapterOption(Context context, int id, ArrayList<ModelTitleValue> titleValues) {
			super(context, id, titleValues);
			this.titleValues = titleValues;
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.popup_spinner_row, null);
			
			TextView text = (TextView) convertView.findViewById(R.id.text_option);
			String title = titleValues.get(position).title;

			if (deviceDip.equals(title) ){
				text.setText(title + getResources().getString(R.string.this_device));
			}else{
				text.setText(title);
			}
			 
			if (mCurrentTitleValue.value.equals(titleValues.get(position).value)) {
				ImageView image = (ImageView) convertView.findViewById(R.id.image_button);
				image.setImageResource(R.drawable.setting_check);
				text.setTextColor(getResources().getColor(R.color.select_color));

			}
			
			return convertView;
		}
		
	}
	
	/**
	 * ���� ����� dip �������� ��´�
	 * @param activity
	 * @param key
	 * @return
	 */
	public String getDeviceDip() {
        DisplayMetrics displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        float deviceDensityDIP = displayMetrics.densityDpi;
        int i = (int) deviceDensityDIP;
        return Integer.toString(i);
	}
}
